<template>
  <el-dialog :title="tableTitle" v-model="props.dialogVisible" width="35%" top="15vh" center @open="tableOpen"
    :before-close="cancelClick">
    <span v-if="titleIDText" class="titleID">（{{ titleIDText }}）</span>
    <div class="bodyBox">
      <el-table class="tableCss" v-loading="tableLoading" border :data="ruleForm"
        :default-sort="{ prop: 'date', order: 'descending' }" height="calc(100vh - 475px)"
        :show-overflow-tooltip="true">
        <el-table-column fixed align="center" label="序号" width="60">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="lang" label="语言" width="">
          <template #default="scope">
            {{ scope.row.lang }}
          </template>
        </el-table-column>
        <el-table-column prop="style_name" label="产品名称" width="">
          <template #default="scope">
            <el-input v-model="scope.row.style_name" placeholder="请输入" style="width: 92%;" /><span
              v-if="scope.$index + 1 === 1" style="color: red;">*</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="style_avatar_url" label="款式预览图" v-if="tableTitle === '编辑款式'" width="">
          <template #default="scope">
            <div class="imgUp">
              <el-upload class="avatar-uploader"
                :http-request="(options: any) => handleCustomUpload(scope, options, scope.row)" :show-file-list="false"
                :before-upload="beforeAvatarUpload">
                <img v-if="scope.row.style_avatar_url" :src="scope.row.style_avatar_url" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                  <Plus />
                </el-icon>
              </el-upload>
              <el-button v-if="scope.row.style_avatar_url" type="danger" :icon="Delete" circle size="small"
                @click="delImg(scope, scope.row)" />
            </div>
          </template>
        </el-table-column> -->
        <el-table-column prop="status" label="上架状态" width="">
          <template #default="scope">
            <el-select v-model="scope.row.status" placeholder="上架状态" style="">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelClick" :loading="btnLoading">关 闭</el-button>
      <el-button type="primary" @click="submitForm()" :loading="btnLoading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, defineProps } from 'vue'
import cloneDeep from 'lodash/cloneDeep';
import { Delete } from '@element-plus/icons-vue'
import type { FormInstance, UploadProps } from 'element-plus'
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false,
  },
  tableLoading: {
    type: Boolean,
    default: false,
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
  tableTitle: {
    type: String,
    default: '',
  },
  ruleForm: {
    type: Array,
    default: () => [],
  },
  titleIDText: {
    type: String,
    default: '',
  },
  productKey: {
    type: String,
    default: '',
  },
})
interface RuleForm {
  lang: string;
  style_name: string;
  status: string;
  product_key: string;
}
let ruleFormData = ref<RuleForm[]>([
  { lang: '', style_name: '', status: '0', product_key: '' },

]);
const options = [
  {
    value: '0',
    label: '已下架',
  },
  {
    value: '1',
    label: '已上架',
  },
]
interface iType {
  [key: string]: string
}
interface RowType {
  [key: string]: string
}
const handleCustomUpload = async (i: iType, options: any, row: RowType) => {

}
// 删除图片
const delImg = (i: iType, row: RowType) => {
  ElMessageBox.confirm('确认删除图片？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    row.style_avatar_url = ''
  }).catch(() => {
    // 点击取消后，不执行任何操作
  });
}
// 上传图片 开始
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type !== 'image/jpg') {
    ElMessage.error('头像格式只能为jpeg、jpg、png!!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 10) {
    ElMessage.error('头像大小不能超过10MB!')
    return false
  }
  return true
}
// 弹窗打开事件
const tableOpen = () => {
  // Object.assign(ruleFormData, props.ruleForm);
}
const ruleFormRef = ref<FormInstance>()
const emits = defineEmits(['beforeClose', 'submitForm']);
// 确定按钮
const submitForm = () => {
  ruleFormData = cloneDeep(props.ruleForm)
  console.log(111, ruleFormData); 
  // productKey
  ruleFormData = ruleFormData.map((item) => {
    return {
      ...item,
      product_key: props.productKey
    }
  })

  ruleFormData[0].lang = 'cn'
  ruleFormData[1].lang = 'en'
  if (!ruleFormData[0].style_name) {
    return ElMessage.error("请输入中文产品名称")
  }

  emits('submitForm', ruleFormData, props.tableTitle);
}
// 弹窗关闭事件
const cancelClick = () => {
  // 清空表单
  ruleFormRef.value?.resetFields();
  emits('beforeClose', false);
}
// 显式暴露方法给父组件
defineExpose({
  cancelClick
});
</script>
<style scoped lang="scss">
.titleID {
  width: 100%;
  display: inline-block;
  margin: 10px auto;
  text-align: right;
}

.tableCss {
  width: 100%;

  .doubleRow {
    color: #bababb;
  }
}

.imgUp {
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.avatar-uploader .avatar) {
  width: 50px;
  height: 50px;
  display: block;
}

:deep(.avatar-uploader) {
  width: 55px;
  display: inline-block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
</style>